/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Entity.Dosen;
import Entity.DosenPemilikHaki;
import Entity.PublikasiHaki;
import Entity.User;
import Model.DaftarPemilikHaki;
import Model.DaftarPublikasiHaki;
import Model.DosenModel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author user
 */
public class TambahPublikasiHaki extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();

        ArrayList ListRole = new ArrayList();
        ListRole = (ArrayList) session.getAttribute("role");

        boolean bukanRole = true;
        if (session.getAttribute("role") != null) {
            for (int i = 0; i < ListRole.size(); i++) {
                if (ListRole.get(i).equals("dosen")) {
                    bukanRole = false;
                }
            }
        }

        if (session.getAttribute("role") == null) {
            Login servlet = new Login();
            servlet.processRequest(request, response);
        } else if (bukanRole) {
            request.setAttribute("menu", "home");
            request.setAttribute("submenu", "");
            RequestDispatcher dis = request.getRequestDispatcher("AccessDenied.jsp");
            dis.forward(request, response);
        } else {

            PublikasiHaki pubHaki = new PublikasiHaki();
            DaftarPublikasiHaki daftarHaki = new DaftarPublikasiHaki();

            String status = "";
            if (request.getParameter("status") != null) {
                status = request.getParameter("status");
            }

            if (status.equals("submit")) {

                Calendar c = Calendar.getInstance();
                c.setTime(new Date());
                int Month = c.get(Calendar.MONTH) + 1;
                String tanggal = c.get(Calendar.DATE) + "" + Month + "" + c.get(Calendar.YEAR) + "_" + c.get(Calendar.HOUR_OF_DAY) + "" + c.get(Calendar.MINUTE);

                int jmlDosenIn = 0, jmlDosenEks = 0;

                pubHaki.setJudulPatenHaki(request.getParameter("judul"));
                pubHaki.setNoPaten(request.getParameter("noPaten"));
                pubHaki.setStatusKemajuan(request.getParameter("statusKemajuan"));
                pubHaki.setTanggalPendaftaranPaten(request.getParameter("tanggalMulai"));
                jmlDosenIn = Integer.parseInt(request.getParameter("jmlDosenIn"));
                jmlDosenEks = Integer.parseInt(request.getParameter("jmlDosenEks"));

                User user = new User();
                String idUser = (String) session.getAttribute("id_user");
                user.setIdUser(idUser);

                pubHaki.setUniqueKey(tanggal + Math.random());
                pubHaki.setUserPengupload(user);

                for (int i = 0; i < ListRole.size(); i++) {
                    if (ListRole.get(i).equals("dosen")) {
                        Dosen dosen = new Dosen();
                        dosen.setUser(user);

                        DosenPemilikHaki dosenPengupload = new DosenPemilikHaki();

                        dosenPengupload.setStatusPengupload("1");
                        dosenPengupload.setPublikasiKarya(pubHaki);
                        dosenPengupload.setDosen(dosen);
                        session.setAttribute("dosenPengupload", dosenPengupload);

                    }
                }

                session.setAttribute("pubHaki", pubHaki);
                session.setAttribute("jmlDosenIn", jmlDosenIn);
                session.setAttribute("jmlDosenEks", jmlDosenEks);

                DosenModel dosenModel = new DosenModel();
                List<Dosen> listDosen = new ArrayList<Dosen>();
                listDosen = dosenModel.DaftarDosen();

                request.setAttribute("dosen", listDosen);
                request.setAttribute("menu", "ppm");
                request.setAttribute("submenu", "karyaHaki");

                RequestDispatcher dis = request.getRequestDispatcher("ppm/halamanPemilikHaki.jsp");
                dis.forward(request, response);

            } else if (status.equals("submitAnggota")) {

                int jumlahDosen = Integer.parseInt((String) "" + session.getAttribute("jmlDosenIn"));
                int jmlDosenEks = Integer.parseInt((String) "" + session.getAttribute("jmlDosenEks"));

                pubHaki = (PublikasiHaki) session.getAttribute("pubHaki");

                boolean sukses = daftarHaki.tambahPublikasiHaki(pubHaki);
                String pesan = null;
                String isiPesan = null;
                if (sukses) {
                    pubHaki = daftarHaki.detailPublikasiHaki(pubHaki);
                    DaftarPemilikHaki modelDosenHaki = new DaftarPemilikHaki();
                    DosenPemilikHaki dosenPengupload = (DosenPemilikHaki) session.getAttribute("dosenPengupload");

                    for (int i = 0; i < ListRole.size(); i++) {
                        if (ListRole.get(i).equals("dosen")) {
                            dosenPengupload.setPublikasiKarya(pubHaki);
                            modelDosenHaki.tambahTimDosenPemilikHaki(dosenPengupload);
                        }
                    }

                    for (int i = 1; i <= jumlahDosen; i++) {
                        String idDosen = request.getParameter("dosen[" + i + "]");
                        System.out.println("HEHEHE" + idDosen);
                        User user = new User();
                        user.setIdUser(idDosen);

                        Dosen dosen = new Dosen();
                        dosen.setUser(user);

                        dosenPengupload.setStatusPengupload("0");
                        dosenPengupload.setPublikasiKarya(pubHaki);
                        dosenPengupload.setDosen(dosen);

                        modelDosenHaki.tambahTimDosenPemilikHaki(dosenPengupload);
                    }

                    for (int i = 1; i <= jmlDosenEks; i++) {
                        String namaPemilik = request.getParameter("pemilikEks[" + i + "]");
                        System.out.println("HEHEHE" + namaPemilik);

                        dosenPengupload.setStatusPengupload("0");
                        dosenPengupload.setPublikasiKarya(pubHaki);
                        dosenPengupload.setNamaPeneliti(namaPemilik);

                        modelDosenHaki.tambahTimDosenPemilikHaki(dosenPengupload);
                    }

                    pesan = "sukses";
                    isiPesan = "Data publikasi haki berhasil ditambahkan";

                } else {
                    pesan = "gagal";
                    isiPesan = "Data publikasi haki tidak berhasil ditambahkan";
                }
//                session.setAttribute("pesan", pesan);
//                session.setAttribute("isiPesan", isiPesan);

                request.setAttribute("pesan", pesan);
                request.setAttribute("isiPesan", isiPesan);
                TampilDaftarHaki servlet = new TampilDaftarHaki();
                servlet.processRequest(request, response);

            } else if(status.equals("batal")){
                TampilDaftarHaki servlet = new TampilDaftarHaki();
                servlet.processRequest(request, response);
            }else {

                request.setAttribute("menu", "ppm");
                request.setAttribute("submenu", "karyaHaki");

                RequestDispatcher dis = request.getRequestDispatcher("ppm/halamanTambahPubHaki.jsp");
                dis.forward(request, response);
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
